{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "As expected, this produce a hole in the histogram in the range [0.3, 0.4]\n",
      "\u001b[1m\u001b[34mHistogram\t(entries=100000)\n",
      "13059                                              ▉    \n",
      "   |                                               ▉    \n",
      "   |                                               ▉    \n",
      "   |                                               ▉    \n",
      "   |                                               ▉    \n",
      "   |                                               ▉    \n",
      "   |                                               ▉    \n",
      "   |                                               ▉    \n",
      "   |     ▉▉▉▉                                   ▉▉▉▉▉   \n",
      "   |    ▉▉▉▉▉▉▉▉▉▉▉▉▉     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉  \n",
      "0.00..................................................1.00\n",
      "\u001b[0m\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5da424babd734fd5a9af8553e2cc9125",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Generate a voxel dataset by interpolating a scalar\n",
    "which is only known on a scattered set of points or mesh.\n",
    "Available interpolation kernels are: shepard, gaussian, voronoi, linear.\n",
    "The blue layer is the result of thresholding the volume\n",
    "between 0.3 and 0.4 and assigning it the new value of 0.9\n",
    "\"\"\"\n",
    "from vtkplotter import *\n",
    "import numpy as np\n",
    "\n",
    "npts = 500                       # nr. of points of known scalar value\n",
    "coords = np.random.rand(npts, 3) # range is [0, 1]\n",
    "scals = np.abs(coords[:, 2])     # let the scalar be the z of point itself\n",
    "\n",
    "apts = Points(coords).addPointScalars(scals, name='scals')\n",
    "\n",
    "vol = interpolateToVolume(apts, kernel='shepard', radius=0.2, dims=(90,90,90))\n",
    "vol.c([\"tomato\", \"g\", \"b\"]) # set color transfer functions\n",
    "\n",
    "print('As expected, this produce a hole in the histogram in the range [0.3, 0.4]')\n",
    "vol.threshold(vmin=0.3, vmax=0.4, replaceWith=0.9) # replace voxel value in [vmin,vmax]\n",
    "printHistogram(vol, bins=25, c='b')\n",
    "\n",
    "#write(vol, 'cube.vti')\n",
    "\n",
    "show(apts, vol, axes=0)\n",
    "\n",
    "#try: Open Controls -> Controls -> untick gridAutoFit\n",
    "#     Open Controls -> Objects  -> Volume#1 -> change alpha_coeff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
